package com.wangwei.basic;

import edu.princeton.cs.algs4.StdOut;

public class Queue<Item> {
    private Node first;
    private Node last;
    private int N;

    private class Node{
        Item item;
        Node next;
    }


    public void enqueue(Item item){
        Node oldLast = last;
        last =new Node();
        last.item =item;
        last.next =null;
        if(isEmpty()) first = last;
        else oldLast.next = last;
        N++;
    }

    public Item dequeue(){
        Item item = first.item;
        first = first.next;
        if(isEmpty()) last =null;
        N--;
        return item;
    }

    public boolean isEmpty(){
        return N == 0;
        // 或者 first == null;
    }

    public int size(){
        return N;
    }

    public static void test(String exp){
        Queue<String> s =new Queue<>();
        String[] arr =exp.split(" ");
        for(String item : arr){
            if(!item.equals("-")){
                s.enqueue(item);
            }else if(!s.isEmpty()){
                StdOut.println(s.dequeue()+"");
            }
        }
        StdOut.println("("+s.size()+" left on queue)");
    }

    public static void main(String[] args) {
        String exp="to be or not to - be - - that - - - is";
        test(exp);
    }


}
